<?php
namespace app\admin\controller;
use app\admin\collection\chartCollection;
use app\admin\collection\permissionCollection;
use app\admin\model\Group;
use app\admin\model\Users;
use app\admin\model\Posts;
use app\admin\controller\AdminAuth;
use think\Db;
use think\Validate;
use think\Image;
use think\Request;
class ChartController extends AdminAuth
{
	//模块基本信息
	private $data = array(
		'module_name' => '统计',
		'module_url'  => '/admin/chart/',
		'module_slug' => 'Chart',
	);

    /**
     * [index 获取我的业绩统计]
     * @return [type] [description]
     */
    public function index($datas='')
    {
        $chart = new \EchartsBuilder\Echarts();
        $chart->tooltip->trigger = 'axis';
        $chart->legend->data = array('认证数','会员数','专业推广','高级推广','豪华推广','企业推广','业绩');
        $chart->toolbox = array(
            'show' => true,
            'feature' => array(
                'mark' => array('show' => true),
                'dataView' => array('show' => true),
                'magicType' => array('show' => true, 'type' => array('line', 'bar')),
                'restore' => array('show' => true),
                'saveAsImage' => array('show' => true),
            )
        );
        $chart->calculable = false;//是否启用拖拽重计算

        //获取当前月份
        $month = date('n');
        $month_list = array(
            '1','2','3','4','5','6','7','8','9','10','11','12'
        );
        $key = array_search($month,$month_list);
        $month_list[$key] = [
            'value' => $month,
            'textStyle' => array(
                'color' => 'red',
                'fontSize' => 30,
                'fontStyle' => 'normal',
                'fontWeight' => 'bold',
            ),
        ];


        $chart->xAxis[] = array(
            'type' => 'category',
            'position' => 'bottom',
            'boundaryGap' => true,
            'axisLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => 'green',
                    'type' => 'solid',
                    'width' => 2,
                ),
            ),
            'axisTick' => array(
                'show' => true,
                'length' => 10,
                'lineStyle' => array(
                    'color' => 'red',
                    'type' => 'solid',
                    'width' => 2,
                ),
            ),
            'axisLabel' => array(
                'show' => true,
                'interval' => 'auto',
                'rotate' => 45,
                'margin' => 8,
                'formatter' => '{value}月',
                'textStyle' => array(
                    'color' => 'blue',
                    'fontFamily' => 'sans-serif',
                    'fontSize' => 15,
                    'fontStyle' => 'italic',
                    'fontWeight' => 'bold',
                ),
            ),
            'splitLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => '#483d8b',
                    'type' => 'dashed',
                    'width' => 1,
                ),
            ),
            'splitArea' => array(
                'show' => true,
                'areaStyle' => array(
                    'color' => array('rgba(144,238,144,0.3)','rgba(135,200,250,0.3)')
                ),
            ),
            'data' => $month_list
        );
        $chart->xAxis[] = array(
            'type' => 'category',
            'data' => array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),
        );
        $chart->yAxis[] = array(
            'type' => 'value',
            'position' => 'left',
            'boundaryGap' => array(0,0.1),
            'axisLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => 'red',
                    'type' => 'dashed',
                    'width' => 2,
                ),
            ),
            'axisTick' => array(
                'show' => true,
                'length' => 10,
                'lineStyle' => array(
                    'color' => 'green',
                    'type' => 'solid',
                    'width' => 2,
                ),
            ),
            'axisLabel' => array(
                'show' => true,
                'interval' => 'auto',
                'rotate' => -45,
                'margin' => 18,
                'formatter' => '{value} 人',
                'textStyle' => array(
                    'color' => '#1e90ff',
                    'fontFamily' => 'verdana',
                    'fontSize' => 10,
                    'fontStyle' => 'normal',
                    'fontWeight' => 'bold',
                ),
            ),
            'splitLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => '#483d8b',
                    'type' => 'dotted',
                    'width' => 2,
                ),
            ),
            'splitArea' => array(
                'show' => true,
                'areaStyle' => array(
                    'color' => array('rgba(205,92,92,0.3)','rgba(255,215,0,0.3)')
                ),
            ),
        );
        $chart->yAxis[] = array(
            'type' => 'value',
            'splitNumber' => 10,
            'axisLabel' => array(
                'formatter' => "
            function (value)
            {
                return value + ' 人'
            }
        ",
            ),
            'splitLine' => array(
                'show' => true,
            )
        );
        $rz_arr = chartCollection::getAllMonthAuthentication(session('uid'),[],date('Y'));
        $chart->series[] = array(
            'name' => '认证数',
            'type' => 'bar',
            'data' => $rz_arr,
        );
        $_list = chartCollection::getAllMonthMember(session('uid'),[],date('Y'));

        $chart->series[] = array(
            'name' => '会员数',
            'type' => 'bar',
            'data' => array_column($_list,'num'),
        );
        $chart->series[] = array(
            'name' => '专业推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num1')
        );
        $chart->series[] = array(
            'name' => '高级推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num2')
        );
        $chart->series[] = array(
            'name' => '豪华推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num3')
        );
        $chart->series[] = array(
            'name' => '企业推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num4')
        );

        $yj_arr = chartCollection::getMonthYj($rz_arr,$_list);
        $chart->series[] = array(
            'name' => '业绩',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => $yj_arr
        );

        $this->assign('datalist',$_list);
        $this->assign('data',$this->data);
        $this->assign('list',$chart->render('advance-custom-id'));

        $datalists = isset($datas) ? $datas : '';

        if(isset($datalists) && strlen($datalists) > 0){
            $datalist=unserialize($datalists);
            $re_list = [];
            foreach($datalist as $sk=>$sv){
                $sv = array_reverse($sv);
                foreach($sv as $ssk=>$ssv){
                    $re_list[$sk][] = $ssv;
                }
            }
            $data = [];
            foreach($re_list as $kk=>$vv){
                Array_unshift($vv,$kk);
                $data[] = $vv;
            }
            $str = "月份,认证数,会员数,专业推广,高级推广,豪华推广,企业推广\n";
            $str = iconv('utf-8', 'gb2312', $str);
            foreach ($data as $key=>&$item) {
                $a = iconv('utf-8', 'gb2312', $item[0]);
                $b = iconv('utf-8', 'gb2312', $rz_arr[$key]);
                $c = iconv('utf-8', 'gb2312', $item[1]);
                $d = iconv('utf-8', 'gb2312', $item[2]);
                $e = iconv('utf-8', 'gb2312', $item[3]);
                $f = iconv('utf-8', 'gb2312', $item[4]);
                $g = iconv('utf-8', 'gb2312', $item[5]);
                $str .= $a . "," . $b . "," . $c . "," . $d . "," . $e . "," . $f . "," . $g . "\n";
            }
            $filename = 'personal.csv';

            header("Content-type:text/csv");
            header("Content-Disposition:attachment;filename=" . $filename);
            header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
            header('Expires:0');
            header('Pragma:public');
            echo $str;exit;

        }else{
            return $this->fetch();
        }

    }

    /**
     * [index 获取部门业绩]
     * @return [type] [description]
     */
    public function groupYj($group_id=0,$user_id=0,$datas=0)
    {
        /*
         * 判断当前登录者身份
         */
        //判断是否是超级管理员
        $is_admin = session('is_admin');
        $groups = $users = [];
        if($is_admin == 1){
            //获取所有部门
            $groups = Group::where('status',1)->field('group_name,id')->select();
            $users = permissionCollection::getUsersByGroupId($group_id);
        }else{
            $users = permissionCollection::getUsersByGroupId(session('user_group'));
        }

        $chart = new \EchartsBuilder\Echarts();
        $chart->tooltip->trigger = 'axis';
        $chart->legend->data = array('认证数','会员数','专业推广','高级推广','豪华推广','企业推广','业绩');
        $chart->toolbox = array(
            'show' => true,
            'feature' => array(
                'mark' => array('show' => true),
                'dataView' => array('show' => true),
                'magicType' => array('show' => true, 'type' => array('line', 'bar')),
                'restore' => array('show' => true),
                'saveAsImage' => array('show' => true),
            )
        );
        $chart->calculable = false;//是否启用拖拽重计算

        //获取当前月份
        $month = date('n');
        $month_list = array(
            '1','2','3','4','5','6','7','8','9','10','11','12'
        );
        $key = array_search($month,$month_list);
        $month_list[$key] = [
            'value' => $month,
            'textStyle' => array(
                'color' => 'red',
                'fontSize' => 30,
                'fontStyle' => 'normal',
                'fontWeight' => 'bold',
            ),
        ];


        $chart->xAxis[] = array(
            'type' => 'category',
            'position' => 'bottom',
            'boundaryGap' => true,
            'axisLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => 'green',
                    'type' => 'solid',
                    'width' => 2,
                ),
            ),
            'axisTick' => array(
                'show' => true,
                'length' => 10,
                'lineStyle' => array(
                    'color' => 'red',
                    'type' => 'solid',
                    'width' => 2,
                ),
            ),
            'axisLabel' => array(
                'show' => true,
                'interval' => 'auto',
                'rotate' => 45,
                'margin' => 8,
                'formatter' => '{value}月',
                'textStyle' => array(
                    'color' => 'blue',
                    'fontFamily' => 'sans-serif',
                    'fontSize' => 15,
                    'fontStyle' => 'italic',
                    'fontWeight' => 'bold',
                ),
            ),
            'splitLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => '#483d8b',
                    'type' => 'dashed',
                    'width' => 1,
                ),
            ),
            'splitArea' => array(
                'show' => true,
                'areaStyle' => array(
                    'color' => array('rgba(144,238,144,0.3)','rgba(135,200,250,0.3)')
                ),
            ),
            'data' => $month_list
        );
        $chart->xAxis[] = array(
            'type' => 'category',
            'data' => array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),
        );
        $chart->yAxis[] = array(
            'type' => 'value',
            'position' => 'left',
            'boundaryGap' => array(0,0.1),
            'axisLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => 'red',
                    'type' => 'dashed',
                    'width' => 2,
                ),
            ),
            'axisTick' => array(
                'show' => true,
                'length' => 10,
                'lineStyle' => array(
                    'color' => 'green',
                    'type' => 'solid',
                    'width' => 2,
                ),
            ),
            'axisLabel' => array(
                'show' => true,
                'interval' => 'auto',
                'rotate' => -45,
                'margin' => 18,
                'formatter' => '{value} 人',
                'textStyle' => array(
                    'color' => '#1e90ff',
                    'fontFamily' => 'verdana',
                    'fontSize' => 10,
                    'fontStyle' => 'normal',
                    'fontWeight' => 'bold',
                ),
            ),
            'splitLine' => array(
                'show' => true,
                'lineStyle' => array(
                    'color' => '#483d8b',
                    'type' => 'dotted',
                    'width' => 2,
                ),
            ),
            'splitArea' => array(
                'show' => true,
                'areaStyle' => array(
                    'color' => array('rgba(205,92,92,0.3)','rgba(255,215,0,0.3)')
                ),
            ),
        );
        $chart->yAxis[] = array(
            'type' => 'value',
            'splitNumber' => 10,
            'axisLabel' => array(
                'formatter' => "
            function (value)
            {
                return value + ' 人'
            }
        ",
            ),
            'splitLine' => array(
                'show' => true,
            )
        );
        $rz_arr = chartCollection::getAllMonthAuthentication($user_id,[],date('Y'));
        $chart->series[] = array(
            'name' => '认证数',
            'type' => 'bar',
            'data' => $rz_arr,
        );
        $_list = chartCollection::getAllMonthMember($user_id,[],date('Y'));

        $chart->series[] = array(
            'name' => '会员数',
            'type' => 'bar',
            'data' => array_column($_list,'num'),
        );
        $chart->series[] = array(
            'name' => '专业推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num1')
        );
        $chart->series[] = array(
            'name' => '高级推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num2')
        );
        $chart->series[] = array(
            'name' => '豪华推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num3')
        );
        $chart->series[] = array(
            'name' => '企业推广',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => array_column($_list,'num4')
        );

        $yj_arr = chartCollection::getMonthYj($rz_arr,$_list);
        $chart->series[] = array(
            'name' => '业绩',
            'type' => 'line',
            'yAxisIndex' => 1,
            'data' => $yj_arr
        );

//echo $group_id,$user_id,$datas;exit;
        $this->assign('data',$this->data);
        $this->assign('groups',$groups);
        $this->assign('user_id',$user_id);
        $this->assign('group_id',$group_id);
        $this->assign('is_admin',$is_admin);
        $this->assign('users',$users);
        $this->assign('datalist',$_list);
        $this->assign('list',$chart->render('advance-custom-id'));
        $datalists = isset($datas) ? $datas : '';

        if(isset($datalists) && strlen($datalists) > 3){
            $datalist=unserialize($datalists);
            $re_list = [];
            foreach($datalist as $sk=>$sv){
                $sv = array_reverse($sv);
                foreach($sv as $ssk=>$ssv){
                    $re_list[$sk][] = $ssv;
                }
            }
            $data = [];
            foreach($re_list as $kk=>$vv){
                Array_unshift($vv,$kk);
                $data[] = $vv;
            }
            $str = "月份,认证数,会员数,专业推广,高级推广,豪华推广,企业推广\n";
            $str = iconv('utf-8', 'gb2312', $str);
            foreach ($data as $key=>&$item) {
                $a = iconv('utf-8', 'gb2312', $item[0]);
                $b = iconv('utf-8', 'gb2312', $rz_arr[$key]);
                $c = iconv('utf-8', 'gb2312', $item[1]);
                $d = iconv('utf-8', 'gb2312', $item[2]);
                $e = iconv('utf-8', 'gb2312', $item[3]);
                $f = iconv('utf-8', 'gb2312', $item[4]);
                $g = iconv('utf-8', 'gb2312', $item[5]);
                $str .= $a . "," . $b . "," . $c . "," . $d . "," . $e . "," . $f . "," . $g . "\n";
            }
            $filename = 'groupExcel.csv';

            header("Content-type:text/csv");
            header("Content-Disposition:attachment;filename=" . $filename);
            header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
            header('Expires:0');
            header('Pragma:public');
            echo $str;exit;

        }else{
            return $this->fetch();
        }
    }


    /**
     * [根据部门ID查询出所有用户]
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUsersByGroupId(Request $request)
    {
        $group_id = $request->param('group_id');
        $list = permissionCollection::getUsersByGroupId($group_id);
        $return_data['data']   = $list;
        $return_data['code']    = '200';
        $return_data['message'] = '操作成功';
        echo json_encode($return_data);exit;
    }


}